#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/11/6 19:54
# @Author  : 作者名:张铁君
# @Site    : 
# @File    : LoginPage.py
# @Project : h5_auto_test
# @Software: PyCharm
import time

import pyautogui
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

from base.HomeBase import HomeBase
from base.ObjectMap import ObjectMap
from logs.log import log


class TestPrintPage(ObjectMap):
    def menu_path(self, menu_name):
        return "//span[text()='" + menu_name + "']|//li[contains(text(),'" + menu_name + "')]"

    def click_menu(self, driver, menu_name):
        log.info("输入：" + menu_name)
        menu = self.menu_path(menu_name)
        return self.element_click(driver, By.XPATH, menu)

    def to_print(self, driver):
        self.element_click(driver, By.XPATH, "//div[contains(text(),'审批中')]")
        self.element_click(driver, By.XPATH,
                           "//*[@id=\"app\"]/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div[3]/div/div/div[4]/div[2]/table/tbody/tr[1]/td[1]/div/div/a[3]")
        time.sleep(2)
        # iframe = driver.find_element(By.XPATH, "//iframe")
        # driver.switch_to.frame("print_iframe")
        # self.element_click(driver, By.XPATH, "//embed")
        # time.sleep(1)
        self.save_screenshot1(driver)
        # 1432,260     1920,1080

        screen_size = pyautogui.size()

        x = 1432 / 1920 * screen_size.width
        # pyautogui.moveTo(1491, 304, duration=0.1)
        pyautogui.leftClick(x, 260, duration=0.1)
        time.sleep(0.5)
        pyautogui.press('enter')

        # driver.find_element(By.XPATH, "//embed").send_keys(Keys.CONTROL, 'p');
        # driver.execute_script("document.getElementsByTagName('embed')[0].print()");
        # print(driver.window_handles)
        # driver.execute_script("""
        #     function sleep(ms) {
        #       return new Promise(resolve => setTimeout(resolve, ms));
        #     }
        #     var w = window.open();
        #     html=document.getElementById('print_iframe').contentWindow.window.document.getElementsByTagName('embed')[0].outerHTML
        #     w.document.write(html);
        #     //await sleep(3000);
        #     //w.window.print();
        #     //w.document.close();
        # """)

        # print(driver.window_handles)
        # driver.switch_to.window(driver.window_handles[1])
        # driver.find_element(By.XPATH, "//embed").send_keys(Keys.CONTROL, 'p')
        # time.sleep(3)
        # self.element_click(driver, By.CSS_SELECTOR, "//body")
        # driver.execute_script("window.print();")
        # driver.execute_script("alert(0);")
        # pyautogui.hotkey("ctrl", "p")
        # driver.execute_script(
        #     "document.getElementById('print_iframe').focus();"
        #     "document.getElementById('print_iframe').contentWindow.print();")
        # driver.execute_script("var old=document.body.innerHtml;var iframeHtml=document.getElementById('print_iframe').outerHTML;document.body.innerHtml=iframeHtml;window.print();document.body.innerHtml=old;")
        time.sleep(1)
        pass
